<template>
  <div class="common-layout__my-videos">
    <div>
      <div>我的投稿</div>
      <div>
        <el-radio-group v-model="page.sortType" size="small">
          <el-radio-button :label="1">最近发布</el-radio-button>
          <el-radio-button :label="2">最多播放</el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div v-if="videos.length === 0">
      <el-empty description="没有投稿"/>
    </div>
    <div v-else class="videos-list">
      <div>
        <div v-for="item in videos">
          <el-container>
            <el-main>
              <VideoCover1 :videoInfo="item"></VideoCover1>
            </el-main>
            <el-footer>
              <div class="video-card-footer">
                <RouterLink
                    class="link text-hidden__1 black"
                    :title="item.title"
                    :to="`/video/${item.id}`"
                >
                  {{ item.title }}
                </RouterLink>
                <div>
                  <i>{{ item.createTime }}</i>
                </div>
              </div>
            </el-footer>
          </el-container>
        </div>
      </div>
      <el-pagination
          small
          background
          layout="prev, pager, next"
          :page-size="page.pageSize"
          @current-change="pageChange"
          :total="pageTotal"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted, onUnmounted } from "vue";
import VideoCover1 from "@/components/VideoCover1.vue";
import videoApi from "@/api/video";
import { useRoute } from "vue-router";
import { Video, PageByUserId } from "@/types/video";
import { PageResp } from "@/types/page";
import { Result } from "@/types/common";

const route = useRoute();
const videos = ref(<Video[]>[]);
const page = ref(<PageByUserId>{
  userId: route.params.id,
  pageIndex: 1,
  pageSize: 10,
  sortType: 1
});
const pageTotal = ref<number>();

// 分页获取用户发布的视频
const pageChange = (value: number) => {
  page.value.pageIndex = value;
  videoApi.getByUserId(page.value).then((res: Result<PageResp<Video>>) => {
    if (res.code === 0) {
      videos.value = res.data.records;
      pageTotal.value = res.data.total;
    }
  });
};

const watchType = watch(
  () => page.value.sortType,
  (val) => {
    pageChange(1);
  },
  {immediate: true}
);

const watchRoute = watch(
  () => route.params.id,
  (val) => {
    page.value.userId = String(val);
    pageChange(1);
  },
  {immediate: true}
)

onMounted(() => {
  pageChange(1);
});

onUnmounted(() => {
  watchType();
  watchRoute();
});
</script>

<style lang="scss" scoped>
.common-layout__my-videos {
  .videos-list {
    & > div:first-child {
      display: grid;
      grid-template-columns: repeat(4, 25%);

      & > * {
        padding: 10px;
      }
    }
  }

  .el-main,
  .el-footer {
    padding: revert;
    overflow: hidden;
    text-align: left;
  }

  .video-card-footer {
    position: relative;
    width: 100%;
    height: 32px;
    color: black;
    font-weight: 500;

    & > div:first-child {
      text-overflow: ellipsis;
      overflow: hidden;
      word-break: break-all;
      white-space: nowrap;
    }

    & > div:last-child {
      display: flex;
      align-items: flex-end;
      position: absolute;
      bottom: 0;
      color: #696969;
      line-height: 15px;

      & > div {
        max-width: 80px;
        font-size: 13px;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
        white-space: nowrap;
      }

      & > i:last-child {
        font-size: 12px;
      }
    }
  }
}
</style>
